package com.spm.librarianmanagersystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.MemberMapper;
import com.spm.librarianmanagersystem.pojo.Borrow;
import com.spm.librarianmanagersystem.pojo.Member;
import com.spm.librarianmanagersystem.service.*;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.utils.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 苏成
 * @Date: 2022/04/05/11:49
 * @Description:
 */
@RestController
@RequestMapping("/api/admin")
public class UserManageController {

    @Autowired
    private UserManageService userManageService;

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private ReturnService returnService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    ReservationService service;

    @GetMapping("/user/reservation")
    public JsonResult<JSONArray> getReservations(@RequestParam("user_id") Integer userID) {
        Member member = memberMapper.getMemberByUserId(userID);
        return new JsonResult<>(service.getReservations(member.getId()));
    }

    @GetMapping("/user/detail")
    public JsonResult getAUser(@RequestParam("user_id") int user_id){
        return new JsonResult(userManageService.getUser(user_id));
    }

    @RequestMapping(value="/user/update", method = RequestMethod.PUT)
    public JsonResult updateUser(@RequestBody Map map){
        userManageService.updateUser(map);
        return new JsonResult();
    }

    /*@PostMapping("/user/add")
    public JsonResult addUser(@RequestBody Map map){
        userManageService.addUser(map);
        return new JsonResult();
    }*/

    @RequestMapping(value = "/user/delete", method = RequestMethod.DELETE)
    public JsonResult deleteUser(@RequestBody List<Integer> user_ids){
        userManageService.deleteUser(user_ids);
        return new JsonResult();
    }

    // 管理员登录后获取全部的图书信息
    @GetMapping("/user/getAll")
    public JsonResult<JSONArray> getAll(@RequestParam("page_size") int page_size, @RequestParam("current_page") int current_page){
        return new JsonResult<>(userManageService.getAllMember(page_size, current_page ));
    }

    // 管理员登录之后获取全部的借阅信息
    @GetMapping("/borrowings/getAll")
    public JsonResult getAllBorrow(){
//        try{
//            return new JsonResult(userManageService.getAllBorrow() );
//        }catch (Exception e){
//            return new JsonResult(1,e.getMessage());
//        }
        return new JsonResult(userManageService.getAllBorrow() );
    }


    // 管理员还书
    @PostMapping("/borrowings/return")
    public JsonResult returnBook(@RequestParam("user_id") String user_id, @RequestParam("borrowing_id") Integer borrowing_id){
        try {
            String msg = returnService.returnBook(user_id, borrowing_id);
            return new JsonResult<>(0, msg);
        } catch (Exception e) {
            return new JsonResult<>(1, e.getMessage());
        }
    }

    @GetMapping("/user/borrowing")
    public JsonResult returnBorrowing(@RequestParam("user_id") Integer userID) {

        try {
            Member member = memberMapper.getMemberByUserId(userID);
            JSONArray borrowingList = borrowService.getBorrowingList(member.getId());
            return new JsonResult<>(borrowingList, 0, "successful");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult<>(1, e.getMessage());
        }
    }

}
